package com.gitee.wsl.compose.ui.richtext.content.span

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.text.style.ResolvedTextDirection
import com.gitee.wsl.compose.ui.richtext.content.ParagraphContentDrawer
import com.gitee.wsl.compose.ui.richtext.content.ParagraphLayoutInfo

/**
 * Implementation of an [ParagraphContentDrawer] to
 * render an icon at start of the paragraph.
 */
private class IconParagraphContentDrawer(
    val image: ImageBitmap?
) : ParagraphContentDrawer {

    override fun onDraw(
        drawScope: DrawScope,
        layoutInfo: ParagraphLayoutInfo
    ) {
        if (image == null) return

        drawScope.drawImage(
            image = image,
            topLeft = Offset(
                x = if (layoutInfo.direction == ResolvedTextDirection.Rtl) {
                    layoutInfo.x - image.width
                } else {
                    layoutInfo.x
                },
                y = layoutInfo.top
            )
        )
    }
}
